// pages/videos/videos.js
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast';

const db = wx.cloud.database();
const app = getApp();
const movies_col = db.collection('movies');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        tvLists: [],
        selectImg: "",
        showShare: false,
        shownoShare: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 状态栏样式
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#000000',
          });
        
        this.loadtvList();
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let signId = app.globalData.userId;
        let newCode = app.globalData.code;
        if(signId !== "" && newCode !== "") {
            wx.showShareMenu({
                withShareTicket: true,
                menus: ['shareAppMessage', 'shareTimeline']
            });
            this.setData({
                showShare: true,
                shownoShare: false,
            });
        }else{
            wx.hideShareMenu({
                menus: ['shareAppMessage', 'shareTimeline']
            }); 
        }
    },

    // 请求数据
    async loadtvList () {
        let res = await movies_col.get();
        this.setData({
            tvLists: res.data
        })
    },

    // 点击视频跳转详情
    jumpPage: function (event) {
        Toast.loading({
            duration: 1000,
            message: '加载中...',
            forbidClick: true,
            onClose: () => {
              // 点击新闻列表项，携带id跳转新闻详情页面
              var id = event.currentTarget.dataset.videoid;
              wx.navigateTo({
                url: '/pages/tvDetails/tvDetails?id='+id,
              })
            },
          });
    },

    // 点击收藏事件
    collectDo: function (e) {
      var self = this;
      let signId = app.globalData.userId;
      let newCode = app.globalData.code;
      if(signId !== "" && newCode !== "") {
        var newId = e.currentTarget.dataset.videoid;
        var newIndex = e.currentTarget.dataset.index;
        var item = self.data.tvLists[newIndex];
        item.collected = !item.collected;
  
        if(item.collected === true){
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 2000
          })        
        }else{
          wx.showToast({
            title: '取消收藏成功',
            icon: 'success',
            duration: 2000
          })        
        }
  
        movies_col.doc(newId).update({
          data:{
            collected: item.collected,
            userId: signId,
          },
          success: function(res) {
            console.log("...")
          }
        });
        self.setData({
          tvLists: self.data.tvLists
        });
      }else{
        wx.showToast({
          title: '未登录!',
          icon: "error",
          duration: 2000
        });   
      }
      
    },

    // 分享给微信好友
    onShareAppMessage:function(e) {
        var idOne = e.target.dataset.tvid;
        var titleOne = e.target.dataset.title
        return {
          title: titleOne,
          path: '/pages/tvDetails/tvDetails?id=' + idOne,//这里是被分享的人点击进来之后的页面
        }
    },

    // 分享到朋友圈
    onShareTimeline: function(){
        return{
            title: "视频分享", //字符串  自定义标题
            imageUrl:"../../images/news.png"   //图片地址
        }
    },

    // 分享朋友圈按钮
    sharePyq: function() {
      wx.showToast({
        title: '未实现',
        icon: "error",
        duration: 2000
      });  
    },

    // 未登录的分享按钮事件
    noShare: function() {
      wx.showToast({
          title: '未登录!',
          icon: "error",
          duration: 2000
        });  
  },
   
})